package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.annotaion.SystemControllerLog;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ldl.plus
 * @date 2020年04月24日  11:37
 * 角色控制
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference//查找服务
    private RoleService roleService;

    //编辑检查项
    @RequestMapping("/edit")
    @SystemControllerLog("编辑角色失败")
    public Result edit(@RequestBody Role role, Integer[] menuIds, Integer[] permissionIds) {
        roleService.edit(role, menuIds, permissionIds);
        return new Result(true, "编辑角色成功");
    }

    //新增检查组
    @RequestMapping("/add")
    @SystemControllerLog("新增角色失败")
    public Result add(@RequestBody Role role, Integer[] menuIds, Integer[] permissionIds) {
        roleService.add(role, menuIds, permissionIds);
        return new Result(true, "新增角色成功");//新增成功
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return roleService.pageQuery(queryPageBean);
    }

    //根据ID查询检查组
    @RequestMapping("/findById")
    @SystemControllerLog("查询角色失败")
    public Result findById(Integer id) {
        Role role = roleService.findById(id);
        return new Result(true, "查询角色成功", role);//查询成功
    }

    //根据检查组ID查询检查组包含的多个检查项ID
    @RequestMapping("/findMenuIdsByRoleId")
    @SystemControllerLog("查询失败")
    public Result findMenuIdsByRoleId(Integer id) {
        List<Integer> menuIds = roleService.findMenuIdsByRoleId(id);
        return new Result(true, "查询成功", menuIds);//查询成功
    }

    @RequestMapping("/findPermissionIdsByRoleId")
    @SystemControllerLog("查询失败")
    public Result findPermissionIdsByRoleId(Integer id) {
        List<Integer> permissionIds = roleService.findPermissionIdsByRoleId(id);
        return new Result(true, "查询成功", permissionIds);//查询成功
    }

    //查询所有检查组
    @RequestMapping("/findAll")
    @SystemControllerLog("查询失败")
    public Result findAll() {
        List<Role> list = roleService.findAll();
        return new Result(true, "查询成功", list);//查询成功
    }

    @PreAuthorize("hasAuthority('ROLE_DELETE')")//权限校验
    @RequestMapping("/delete")
    @SystemControllerLog("删除权限失败")
    public Result delete(Integer id) {
        roleService.deleteById(id);
        return new Result(true, "删除权限成功");
    }
}
